﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameSushi.TweenEngine
{
    class TweenManager
    {
        private readonly List<BaseTween> objects = new List<BaseTween>(20);

        public static void SetAutoRemove(BaseTween _object, bool value)
        {
            _object.isAutoRemoveEnabled = value;
        }

        public static void SetAutoStart(BaseTween _object, bool value)
        {
            _object.isAutoStartEnabled = value;
        }

        private bool isPaused = false;

        public virtual TweenManager Add(BaseTween tween) {
            
            if (!objects.Contains(tween))
            {
                objects.Add(tween);
            }
            if (tween.isAutoStartEnabled)
            {
                tween._Start();
            }
            return this;

        }

        public virtual void KillAll() {
            for (int i = 0, n = objects.Count; i < n; i++) {
                BaseTween tween = objects[i];
                if (tween.IsFinished())
                    tween.Kill();
                else
                {
                    tween.ForceEndValues();
                    tween.Kill();
                }
            }
        }

        public virtual void Pause()
        {
            isPaused = true;
        }

        public virtual void Resume()
        {
            isPaused = false;
        }

        public virtual void Update(float deltaTime) {
            for (int i = objects.Count - 1; i >= 0; i--)
            {
                BaseTween obj = objects[i];
                if (obj.IsFinished())
                {
                    objects.RemoveAt(i);
                    obj.Free();
                }
            }

            if (!isPaused)
            {
                for (int i = 0; i < objects.Count; i++)
                {
                    objects[i].Update(deltaTime);
                }
            }
        }

        public virtual int Size()
        {
            return objects.Count;
        }
    }
}
